import modelExtend from 'dva-model-extend'
import * as performancesOfEmployeeService from './services/performancesOfEmployee'
import * as dictionaryService from 'services/dictionary'
import * as employeeService from "../employee/services/employees"
import { pageModel } from 'utils/model'
const { query } = performancesOfEmployeeService

export default modelExtend(pageModel, {
  namespace: 'performancesOfEmployee',
  state: {
    dimension: 'orderType',  //维度筛选字段
    orderType: [],
    adAcquireType: [],
    empList: [],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/performancesOfEmployee') {
          const payload = location.query || { current: 1, size: 10 }
          dispatch({
            type: 'query',
            payload,
          })
          dispatch({
            type: "init"
          })
        }
      })
    },
  },
  effects: {
    * query({ payload = {} }, { call, put }) {
      const response = yield call(query, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    * init({ payload = {} }, { call, put }) {
      const orderTypeResp = yield call(dictionaryService.queryBySn, { sn: 'order_type' })
      const adAcquireTypeResp = yield call(dictionaryService.queryBySn, { sn: 'customer_channel' })

      yield put({
        type: 'updateState', payload: {
          orderType: orderTypeResp.data,
          adAcquireType: adAcquireTypeResp.data
        },
      })
    },

    * searchEmp({ payload = {} }, { call, put }) {
      const response = yield call(employeeService.searchEmp, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empList: data,
          },
        })
      }
    },
  },

  reducers: {
    updateDimension(state, { payload }) {
      return { ...state, ...payload }
    }
  }
})
